﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common.DTO;

namespace Database.Repositories
{
    public class TileRepository : RepositoryBase<Tile>
    {
        //private readonly TileMapper _tileMapper;

        public TileRepository()
        {
            //_tileMapper = new TileMapper();
        }

        public int Create(TileDTO tileDTO)
        {
            var gameObject = Mapper.Map(tileDTO);
            var id = Guid.NewGuid();
            gameObject.Id = id;

            DbContext.TileSet.Add(gameObject);
            return DbContext.SaveChanges();
        }

        public TileDTO Read(Guid id)
        {
            return Mapper.Map(
                DbContext.TileSet.Single(a => a.Id.Equals(id)));
        }

        public TileDTO Update(TileDTO tileDTO)
        {
            var val = Mapper.Map(DbContext.TileSet.Single(a => a.Id.Equals(tileDTO.Id)));
            val.X = tileDTO.X;
            val.Y = tileDTO.Y;

            DbContext.SaveChanges();
            return val;
        }

        public List<TileDTO> GetAllByPlace(PlaceDTO placeDTO)
        {
            var placeEntity = DbContext.PlaceSet.FirstOrDefault(x => x.Id == placeDTO.Id);
            if (placeEntity == null)
                return null;

            var tilesDTO = Mapper.Map(placeEntity.Tile);

            return new List<TileDTO>();
        }
    }
}
